<template>
  <el-dialog
    :title="$t('customer.customerIntentState')"
    :visible.sync="dialogVisible"
    width="340px"
    append-to-body
    :before-close="handleClose"
    @open="open"
  >
    <el-form>
      <el-form-item :label="$t('customer.intentState')">
        <el-select
          v-model="intention"
          class="filter-left"
          :placeholder="$t('common.select', [$t('customer.intentState')])"
          filterable
          size="small"
        >
          <el-option
            v-for="item in options"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="setIntention()">{{
        $t('permission.confirm')
      }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCustomer, setIsIntention } from '@/api/customer'
import { setBlockIsIntention } from '@/api/customer/block'

export default {
  name: 'Intention',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: '',
    closeAction: '',
    source: {
      type: String,
      default: 'customer',
    },
  },
  data() {
    return {
      dialogVisible: this.show,
      intention: 1,
      options: [
        { label: this.$t('customer.have'), value: 1 },
        { label: this.$t('customer.empty'), value: 0 },
      ],
    }
  },
  methods: {
    handleClose(done) {
      this.$emit('update:show', false)
    },
    async open() {
      if (typeof this.id != 'object') {
        let { data } = await getCustomer(this.id)
        this.intention = data.isIntention
      } else {
        this.intention = 1
      }
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    setIntention() {
      let msg = this.$t('customer.confirmChangeCurrentCustomerIntentState')
      if (typeof this.id != 'object') {
        msg = this.$t('customer.confirmChangeCustomIntentState')
      }
      this.$confirm(msg, this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      }).then(async () => {
        let data = { ids: this.id, value: this.intention }
        if (typeof data.ids != 'object') {
          data.ids = [data.ids]
        }
        if (this.source == 'customer') {
          await setIsIntention(data).then((res) => {
            this.$notify({
              title: this.$t('common.success'),
              message: this.$t('common.updateSuccessfully'),
              type: 'success',
              duration: 2000,
            })
            this.$emit('closeAction')
          })
        } else if (this.source === 'block') {
          await setBlockIsIntention(data).then((res) => {
            this.$notify({
              title: this.$t('common.success'),
              message: this.$t('common.updateSuccessfully'),
              type: 'success',
              duration: 2000,
            })
            this.$emit('closeAction')
          })
        }
      })
    },
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
  },
}
</script>

<style scoped></style>

<!--

-->
